package com.leenmvc.core.web.controller;

import com.leenmvc.core.annotation.ApiDescription;
import com.leenmvc.core.annotation.Note;
import com.leenmvc.core.base.BaseController;
import com.leenmvc.core.dao.mysql.wrapper.QueryWrapper;
import com.leenmvc.core.security.User;
import com.leenmvc.core.security.entity.core.SysLogOperation;
import com.leenmvc.core.security.entity.export.LogOperationExcel;
import com.leenmvc.core.security.service.core.SysLogOperationService;
import com.leenmvc.core.utils.ConvertUtils;
import com.leenmvc.core.utils.ExcelUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RestController
@ApiDescription("操作日志控制器")
@RequestMapping("system/sysLogOperation")
public class SysLogOperationController extends BaseController<SysLogOperation> {

    @Autowired
    private SysLogOperationService sysLogOperationService;

    @GetMapping("export")
    @RequiresPermissions("system:sysOperation:export")
    @Note("导出异常信息excel文件")
    public void export(@RequestParam Map<String,Object> params, HttpServletResponse response) throws IOException {
//        String fileName = DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN[0]);
        String fileName = String.valueOf(LocalDateTime.now());
        QueryWrapper wrapper = getQueryWrapper();
        wrapper.le("createDate",params.get("endTime"));
        wrapper.ge("createDate",params.get("startTime"));
        if(null != params.get("status") || !"".equals(params.get("status"))) {
            wrapper.eq("status",params.get("status"));
        }
//        if(null != params.get("AL_creatorName") || !"".equals(params.get("AL_creatorName"))){
//            wrapper.eq("creatorName",params.get("AL_creatorName"));
//        }
        wrapper.eq("creatorName",User.getUsername());
        List<SysLogOperation> logs = sysLogOperationService.query(wrapper);
        List<LogOperationExcel> logErrorExcels = ConvertUtils.sourceToTarget(logs, LogOperationExcel.class);
        ExcelUtils.exportExcel(response, fileName, logErrorExcels, LogOperationExcel.class);
    }

}
